/*
 * Player.java
 * 
 * author: The MomberBan Team
 *    date: 12.09.2008
 * 
 * 
 */

package at.momberban.game.me;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;

/**
 * @author patchinsky
 */
public class Player extends Sprite {
    /**
     * player width
     */
    public static final int PLAYER_WIDTH = 21;

    /**
     * player height
     */
    public static final int PLAYER_HEIGHT = 32;
    
    /**
     * default player speed
     */
    public static final int DEFAULT_SPEED = 4;

    /**
     * default player prefix
     */
    public static final String PLAYER_PREFIX = "Player-";

    /**
     * the maximum player amount, the game is capable of
     */
    public static final int MAX_PLAYERS = 4;
        
    private static Image[] IMAGES = null;

    private static Image[] FRONT_VIEW_IMAGES = null;

    private int id;

    private String name;

    private int speed = DEFAULT_SPEED;

    private int currentDirection;

    private int maxBombs = 5;

    private int bombs;
    
    private int lastPosition;
    
    private int bombsRange;
    
    private int deaths;
    
    private int frags;
    
    private boolean dead;

    static {
        IMAGES = new Image[MAX_PLAYERS];
        FRONT_VIEW_IMAGES = new Image[MAX_PLAYERS];
        for (int i = 1; i <= MAX_PLAYERS; i++) {
            try {
                Logger.log("loading image");
                IMAGES[i-1] = Image.createImage("/Player" + i
                        + ".png");
                FRONT_VIEW_IMAGES[i-1] = Image.createImage(IMAGES[i-1], 0, 0,
                        PLAYER_WIDTH, PLAYER_HEIGHT, Sprite.TRANS_NONE);
            } catch (IOException e) {
                Logger.log("could not create image for player: " + (i));
            }
        }
    }
    
    /**
     * @param id
     * @return
     */
    public static Image getFrontViewImage(int id) {
        return FRONT_VIEW_IMAGES[id - 1];
    }

    /**
     * constructor
     * 
     * @param id
     *            the player id
     * @throws IOException
     */
    public Player(int id) {
        super(IMAGES[id - 1], PLAYER_WIDTH, PLAYER_HEIGHT);

        this.id = id;
        this.name = PLAYER_PREFIX + id;
        this.bombs = this.maxBombs;
        this.bombsRange = Bomb.DEFAULT_EXPLOSION_RANGE;

        currentDirection = Canvas.DOWN;
        Timer animation = new Timer();
        animation.schedule(new PlayerAnimationTask(this), 0, 100);
    }
    
    /**
     * revitalize this player
     */
    public void revitalize() {
        this.bombs = this.maxBombs;
        this.bombsRange = Bomb.DEFAULT_EXPLOSION_RANGE;
        this.dead = false;
        this.speed = DEFAULT_SPEED;
    }

    /**
     * Getter of the property <tt>speed</tt>
     * 
     * @return Returns the speed.
     */

    public int getSpeed() {
        return speed;
    }

    /**
     * Setter of the property <tt>speed</tt>
     * 
     * @param speed
     *            The speed to set.
     */
    public void setSpeed(int speed) {
        this.speed = speed;
    }

    /**
     * Getter of the property <tt>maxBombs</tt>
     * 
     * @return Returns the maxBombs.
     */

    public int getMaxBombs() {
        return maxBombs;
    }

    /**
     * Setter of the property <tt>maxBombs</tt>
     * 
     * @param maxBombs
     *            The maxBombs to set.
     */
    public void setMaxBombs(int maxBombs) {
        this.maxBombs = maxBombs;
    }

    /**
     * @return bombs
     */
    public int getBombs() {
        return bombs;
    }

    /**
     * @param bombs
     *            to set
     */
    public void setBombs(int bombs) {
        this.bombs = bombs;
    }

    /**
     * @return id
     */
    public int getId() {
        return id;
    }

    /**
     * @param direction
     */
    public void setDirection(int direction) {
        currentDirection = direction;
    }

    /**
     * @return
     */
    public int getDirection() {
        return currentDirection;
    }

    /**
     * @param name
     *            to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * @return
     */
    public int getLastPosition() {
        return this.lastPosition;
    }
    
    /**
     * @param lastPosition to set
     */
    public void setLastPosition(int lastPosition) {
        this.lastPosition = lastPosition;
    }
    
    /**
     * @param bombsRange to set
     */
    public void setBombsRange(int bombsRange) {
        this.bombsRange = bombsRange;
    }
    
    /**
     * @return bombsRange
     */
    public int getBombsRange() {
        return bombsRange;
    }
    
    /**
     * @return deaths
     */
    public int getDeaths() {
        return deaths;
    }
    
    /**
     * @param deaths to set
     */
    public void setDeaths(int deaths) {
        this.deaths = deaths;
    }
    
    /**
     * @return frags
     */
    public int getFrags() {
        return frags;
    }
    
    /**
     * @param frags to set
     */
    public void setFrags(int frags) {
        this.frags = frags;
    }
    
    /**
     * @return dead
     */
    public boolean isDead() {
        return dead;
    }
    
    /**
     * @param dead to set
     */
    public void setDead(boolean dead) {
        Media.playKillSound();
        this.dead = dead;
    }
    
    /**
     * Animates a player
     * 
     * @author Tobias Wa�mundt
     * @date 18.09.2008
     */
    private static class PlayerAnimationTask extends TimerTask {

        private Player player;
        private int lastDirection;
        
        /**
         * constructor
         * 
         * @param player
         */
        public PlayerAnimationTask(Player player) {
            this.player = player;
            this.lastDirection = player.getDirection();
        }


        public void run() {
            if (player.getDirection() != lastDirection) {
                if (player.getDirection() == Canvas.DOWN) {
                    player.setFrame(0);
                } else if (player.getDirection() == Canvas.LEFT) {
                    player.setFrame(4);
                } else if (player.getDirection() == Canvas.UP) {
                    player.setFrame(8);
                } else if (player.getDirection() == Canvas.RIGHT) {
                    player.setFrame(12);
                }
            } else if (player.getFrame() % 4 == 3) {
                player.setFrame(player.getFrame() - 3);
            } else {
                player.nextFrame();
            }
            lastDirection = player.getDirection();
        }

    }

}
